package com.vhall.component.controller.redpacket;

import com.vhall.component.controller.redpacket.facade.RedPacketSettingFacade;
import com.vhall.component.controller.room.facade.RoomInnerFacade;
import com.vhall.component.entity.rebroadcast.RedPacketSettingEntity;
import com.vhall.component.entity.redpacket.vo.RedPacketSettingVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.redpacket.service.RedPacketSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author xuyongzheng
 * @since 2021/2/25 10:30
 **/

@Slf4j
@VssTokenAuth
@VhallComponent
public class RedPacketSettingController implements RedPacketSettingFacade {

    @Autowired
    private RoomInnerFacade roomInnerFacade;

    @Autowired
    private RedPacketSettingService redPacketSettingService;

    @Override
    @PostMapping("/setting")
    public RedPacketSettingVO setting(@Validated RedPacketSettingVO vo) {
        if (StringUtils.isNotBlank(vo.getRoomId())) {
            RoomsEntityDTO roomsEntityInfo = roomInnerFacade.getRoomsEntityInfo(vo.getRoomId());
            if (roomsEntityInfo == null) {
                throw new BusinessException(BizErrorCode.EMPTY_ROOM);
            }
        }
        RedPacketSettingEntity entity = redPacketSettingService.setting(vo);
        RedPacketSettingVO result = new RedPacketSettingVO();
        BeanUtils.copyProperties(entity, result);
        result.setRoomId(vo.getRoomId());
        return result;
    }
}

